package com.gaohui.nestedrecyclerview.java.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;


import com.gaohui.nestedrecyclerview.java.view.CategoryView;
import com.gaohui.nestedrecyclerview.java.view.InforListView;

import java.util.ArrayList;

public class CategoryPagerAdapter extends PagerAdapter {

    private ArrayList<InforListView> mViewList;
    private ArrayList<String> mTabList;

    private InforListView mCurrentPrimaryItem = null;


    public CategoryPagerAdapter(ArrayList<InforListView> viewList, ArrayList<String> tabList) {
        mViewList = viewList;
        mTabList = tabList;
    }

    @Override
    public int getCount() {
        return mViewList.size();
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        InforListView inforListView = mViewList.get(position);
        if (container == inforListView.getParent()) {
            container.removeView(inforListView);
        }
        container.addView(inforListView);
        return inforListView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        super.destroyItem(container, position, object);
    }

    @Override
    public void setPrimaryItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        InforListView categoryView = (InforListView) object;
        if (categoryView != mCurrentPrimaryItem) {
            if (mCurrentPrimaryItem != null) {
                mCurrentPrimaryItem.onUserVisibleChange(false);
            }
        }
        categoryView.onUserVisibleChange(true);
        mCurrentPrimaryItem = categoryView;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mTabList.get(position);
    }
}
